package com.qf;

import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

/**
 保存对象数据到redis
 */
public class JedisDemo4 {
    public static void main(String[] args) {
        //创建User对象
        List<User> users = new ArrayList<>();
        User user1 = new User("lisi",20,"男");
        User user2 = new User("lisi",20,"男");
        users.add(user1);
        users.add(user2);

        //获取Jedis连接对象
        Jedis jedis = JedisPoolUtils.getJedis();

        //数据保存操作 (将对象转成字符串保存)
        String usersJson = JSON.toJSONString(users);
        jedis.set("users",usersJson);

        //获取
        String json = jedis.get("users");
        List<User> userList = JSON.parseArray(json, User.class);
        System.out.println(userList);

        //归还连接
        jedis.close();
    }
}
